module atext
import StdEnv, japi

/* A very simple example, that shows how to create a new text area,
   and a button. If you write a number on the text area, and press
   the button, you will get the factorial of the number (if it is
   not very large. The example also shows how to change fonts
   on a text area, or an edit field. For the time being, the only
   fonts available are Times New Roman, Courier, and Helvetia.    */

:: State = {frame::!Framer, text::!TDev, exec:: !TDev}

factorial n
  | n < 1= 1
  | otherwise= n* (factorial (n-1))
  
strfactorial d
   # n= toInt (jgetText d)
   # ans= "Factorial: "+++(toString(factorial n))
   = jsetText d ans

Start world
  # (js, world) = begin_japi world
    (frm, js) = jnewFrame  (XY 90 80) "Factorial" js
    (frm, js) = jverticalLayout frm js
    (frm, js) = vGap frm 20 js
    (txt, js) = jnewTextArea (WH 300 100) 25 3 frm js
    (btx, js) = jnewButton FLOW "exec" frm js
    (frm, js) = jpack frm js
    (frm, js) = jshow frm js
    s= { frame= frm, 
         text= jsetfont (Courier 20) txt, 
         exec= btx}
    js= loop s js
  = end_japi js world
where
  loop :: !State !JapiState -> JapiState
  loop s js
     # (na, js) = jnextaction js
     | na== (act s.frame)= jquit js
     | na== (act s.exec)
           # ans= strfactorial s.text
           = loop {s & text= ans} js
     = loop s js
    
    
 